package com.kevin.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLUtils {
	
	/**
	 * 解析XML文档
	 * 
	 * @param xmlFile
	 * @return XML文档
	 * @throws DocumentException
	 * @throws FileNotFoundException
	 */
	public static Document parse(String xmlFile) throws DocumentException, FileNotFoundException {
		SAXReader reader = new SAXReader();
		Document doc = reader.read(new File(xmlFile));
		return doc;
	}

	/***
	 * 将XML文档输出到控制台
	 * 
	 * @param doc
	 * @throws IOException
	 */
	public static void printDocument(Document doc) throws IOException {
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter writer = new XMLWriter(new OutputStreamWriter(System.out), format);
		writer.write(doc);
		writer.close();
	}

	
	/**
	 * 保存XML文档
	 * 
	 * @param doc
	 * @throws IOException
	 */
	public static void saveDocument(Document doc,String xmlFilePath) throws IOException {
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter writer = new XMLWriter(new FileOutputStream(xmlFilePath), format);
		writer.write(doc);
		writer.close();
	}
	
	
	

	
	public static void main(String[] args) throws Exception {
	    XMLUtils util=new XMLUtils();
	    String src="/Users/kevin/workspace/ESBPrj1.0.0/CoreServices/BusinessServices/MOTPublishService/v1.0.0/MOTPublishService.xml";
	    Document doc = util.parse(src);
	    String xpath="/serviceResource/config/interfaceImpl/tab/config/detail/tab/config/operations";
	    Element operations = (Element) doc.selectSingleNode(xpath);
	    Element e = operations.addElement("row");
	    e.addAttribute("opName", "PublishData");
	    e.addAttribute("opImpl", "/CoreServices/BusinessServices/MOTPublishService/v1.0.0/PortTypePublishData.process");
	    
	    String endpointHTTPFilePath="/Users/kevin/workspace/ESBPrj1.0.0/CoreServices/BusinessServices/MOTPublishService/v1.0.0/PortTypeEndpointHTTP.xml";
	    String endpointHTTPXmlPath="serviceResource/config/interfaceImpl/tab/config/detail/tab/config/epBindings/row[1]/config/epDetail/tab/config/soap";
	    
	    String endpointJMSFilePath="/Users/kevin/workspace/ESBPrj1.0.0/CoreServices/BusinessServices/MOTPublishService/v1.0.0/PortTypeEndpointJMS.xml";
	    String endpointJMSXmlPath="serviceResource/config/interfaceImpl/tab/config/detail/tab/config/epBindings/row[2]/config/epDetail/tab/config/soap/operation";
	    
	    Element endpointHTTPElement = util.parse(endpointHTTPFilePath).getRootElement();
	    Element httpSrc = (Element) doc.selectSingleNode(endpointHTTPXmlPath);
	    httpSrc.add(endpointHTTPElement);
	    
	    Element endpointJMSElement = util.parse(endpointJMSFilePath).getRootElement();
	    Element jmsSrc = (Element) doc.selectSingleNode(endpointJMSXmlPath);
	    jmsSrc.add(endpointJMSElement);
	    
	    util.printDocument(doc);
//	    util.saveDocument(doc,dist);
	}
	

}